/* 
 * (C) 2013 Daniel Garcia <contacto {at} danigarcia.org>
 * 
 * http://danigarcia.org
 * 
 * This file is part of BlackList.
 * 
 * BlackList is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BlackList is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with BlackList.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.danigarcia.android.blacklist.phone;

import java.lang.reflect.Method;

import org.danigarcia.android.blacklist.database.ConfigManager;
import org.danigarcia.android.blacklist.database.DataManager;
import org.danigarcia.android.blacklist.service.BlockingService;

import android.content.Context;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.android.internal.telephony.ITelephony;

/**
 * Class provided to detect when an incoming call is being received and hang up
 * the phone if configured.
 * * * *
 * UPDATE 2013/10/26 : 
 * Class not used anymore because Dual-SIM issues with secondary SIM (it's not 
 * detecting secondary SIM events). Use IncomingCallReceiver instead. 
 * * * *
 * @author Dani
 *
 */
public class IncomingCallListener extends PhoneStateListener {

	private final static String TAG = IncomingCallListener.class.getName();
	
	private Context context;
	private DataManager dataManager;
	private TelephonyManager tManager;
	
	public IncomingCallListener(Context c, DataManager dm, TelephonyManager tm)
	{
		this.context = c;
		this.dataManager = dm;
		this.tManager = tm;
	}
	
	@Override
	  public void onCallStateChanged(int state, String incomingNumber) {
	      switch (state) {
	          case TelephonyManager.CALL_STATE_RINGING:
	          {
	        	  Log.d(TAG, String.format("onCallStateChanged(%s, %s)", state, incomingNumber));
	        	  if(dataManager.exists(incomingNumber))
	        	  {
	        		  // Silence ringer
	        		  //silence();
	        		  
	        		  // If pickup before hanging
	        		  if(ConfigManager.getBoolean(ConfigManager.KEY_PICKUP))
	        			  pickUp();
	        		  
	        		  // Hang up the phone
	        		  hangUp();
	        		  
	        		  // Register into database
	        		  dataManager.insertCall(incomingNumber);
	        		  
	        		  if(context instanceof BlockingService)
	        		  {
	        			  if(ConfigManager.getBoolean(ConfigManager.KEY_SHOW_NOTIFICATIONS))
	        				  ((BlockingService)context).updateNotification(incomingNumber, true, false);
	        		  }
	        	  }
	        	  break;
	          }
	      }
	  }
	
	private void hangUp()
	{
		Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
		Method getITelephony = null;
		
		try {
			getITelephony = telephonyManagerClass.getDeclaredMethod("getITelephony", (Class[])null);
			getITelephony.setAccessible(true);
			ITelephony iTelephony = (ITelephony)getITelephony.invoke(tManager);
			iTelephony.endCall();
		}
		catch(Exception e) {
			Log.e(TAG, "hangUp()");
		}
	}
	
/*	
	private void silence()
	{
		Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
		Method getITelephony = null;
		
		try {
			getITelephony = telephonyManagerClass.getDeclaredMethod("getITelephony", (Class[])null);
			getITelephony.setAccessible(true);
			ITelephony iTelephony = (ITelephony)getITelephony.invoke(tManager);
			iTelephony.silenceRinger();
		}
		catch(Exception e) {
			Log.e(TAG, "silenceRinger()");
		}
	}
*/
	
	private void pickUp()
	{
		Class<TelephonyManager> telephonyManagerClass = TelephonyManager.class;
		Method getITelephony = null;
		
		try {
			getITelephony = telephonyManagerClass.getDeclaredMethod("getITelephony", (Class[])null);
			getITelephony.setAccessible(true);
			ITelephony iTelephony = (ITelephony)getITelephony.invoke(tManager);
			iTelephony.answerRingingCall();
		}
		catch(Exception e) {
			Log.e(TAG, "silenceRinger()");
		}
	}
}
